package fr.umlv.escape_ig.ship;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import fr.umlv.escape_ig.game.GameState;
import fr.umlv.escape_ig.game.UpdateThread;
import fr.umlv.escape_ig.gesture.Gesture;


public class RobotArena extends SurfaceView {

        private SurfaceHolder holder;

        private final GameState mState;
        Gesture gesture;
        
        public RobotArena(Context context,DisplayMetrics ecran) {
                super(context);

                this.holder = getHolder();
                this.mState = new GameState(context,ecran);
                callback();             
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
                
        	       	
                if (event.getActionIndex() == 0 && 
                                event.getActionMasked() == MotionEvent.ACTION_MOVE) {
                        return false;
                }
                
                if (event.getActionIndex() == 0 && 
                                event.getActionMasked() == MotionEvent.ACTION_UP) {
                        mState.playerFire();
                        return true;
                }           	
                      
                
                float x = event.getX(0);
                float y = event.getY(0);
                
                if (mState.player.rect.contains(x, y)) {
                        mState.player.loadWeapon();
                        return true;
                }
                
                mState.player.moveTo(event.getX(0), event.getY(0));
                return true;
        }

        private void callback(){
                getHolder().addCallback(new Callback() {

                        private final UpdateThread thread = new UpdateThread(holder, mState);

                        @Override
                        public void surfaceDestroyed(SurfaceHolder holder) {
                                thread.stopThread();
                        }


                        @Override
                        public void surfaceCreated(SurfaceHolder arg0) {
                                thread.startThread();
                        }

                        @Override
                        public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3){                                   
                        }
                });     
        }

}